package com.boot.notes.rocketmq;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

import java.nio.charset.Charset;
import java.util.Date;
import java.util.List;

/**
 * selectorExpression 默认为 *号即所有 对应生产者的tag
 * maxReconsumeTimes 重试次数，最终失败后会进入死信队列
 * topic：和消费者发送的topic相同
 * consumerGroup：不用和生产者group相同
 */
@Slf4j
@Service
@RocketMQMessageListener(consumerGroup = "my-consumer_test-topic-1", topic = "test-topic-2", selectorExpression = "*")
public class MyConsumer implements RocketMQListener<MessageExt> {
    public void onMessage(MessageExt order) {

        String str = new String(order.getBody(), Charset.forName("UTF-8"));
        log.info("wo消息内容：" + str);
//        JSON.parseObject(str, String.class);
//        System.out.println("///////////////" + JSON.parseObject(str, String.class) + "---");

        log.info(DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN) + " received message: " + order);

    }

    public static void main(String[] args) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("my-consumer_test-topic-3");
        // 设置 NameServer 地址
        consumer.setNamesrvAddr("192.168.0.166:9876");
        // 订阅 Topic
        consumer.subscribe("test-topic-1", "*");
        // 这次回调接口，接收消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt messageExt : list) {
                    String str = new String(messageExt.getBody(), Charset.forName("UTF-8"));
                    log.info("消息内容：" + str);
                }
                try {
                    // 对消息的处理，比如发放优惠券、积分等
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                } catch (Exception e) {
                    // 万一发生数据库宕机等异常，返回稍后重试消息的状态
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }
            }
        });
        consumer.start();
    }
}


///**
// * 指定连接某个MQ集群
// */
//
//@Slf4j
//@Service
//@RocketMQMessageListener(nameServer = "127.0.0.1:9876", topic = "test-topic-1", consumerGroup = "my-consumer_test-topic-3")
//public class MyConsumer implements RocketMQListener<String> {
//    public void onMessage(String message) {
//        log.info("received message: " + message);
//    }
//}


///**
// * 顺序消息消费失败，默认不重试(官方是一直重试)
// */
//@Slf4j
//@Service
//@RocketMQMessageListener(topic = "test-topic-4", consumerGroup = "my-consumer_test-topic-4",
//        consumeMode = ConsumeMode.ORDERLY)
//public class MyConsumer implements RocketMQListener<String> {
//    public void onMessage(String message) {
//        log.info("received message: " + message);
//        int a = 1 / 0;
//    }
//}


///**
// * 配置重试次数 reconsumeTimes = 3
// */
//@Slf4j
//@Service
//@RocketMQMessageListener(topic = "test-topic-4", consumerGroup = "my-consumer_test-topic-5",
//        consumeMode = ConsumeMode.ORDERLY, maxReconsumeTimes = 3)
//public class MyConsumer implements RocketMQListener<MessageExt> {
//    public void onMessage(MessageExt messageExt) {
//        log.info("received message: " + messageExt);
//        int a = 1 / 0;
//    }
//}

///**
// * 配置重试次数 reconsumeTimes = -1 代表一直重试
// */
//@Slf4j
//@Service
//@RocketMQMessageListener(topic = "test-topic-4", consumerGroup = "my-consumer_test-topic-6",
//        consumeMode = ConsumeMode.ORDERLY, maxReconsumeTimes = 5)
//public class MyConsumer implements RocketMQListener<MessageExt> {
//    public void onMessage(MessageExt messageExt) {
//        log.info("received message: " + messageExt);
//        int a = 1 / 0;
//    }
//}